package lockmanager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class TestThread implements Runnable{

	ResourcePool resPool;
	String threadName;
	
	public static List<Long> getresults = Collections.synchronizedList(new ArrayList<Long>());
	
	public static List<Long> putresults = Collections.synchronizedList(new ArrayList<Long>());
	
	public static AtomicInteger startedThreadNumber = new AtomicInteger(0);
	
	public static AtomicInteger finishedThreadNumber = new AtomicInteger(0);
	
	TestThread(String name,ResourcePool resPool){
		this.resPool = resPool;
		this.threadName = name;
	}
	
	@Override
	public void run() {
		
		startedThreadNumber.incrementAndGet();
		
		long init = System.nanoTime();
		
		Object element = resPool.get();
		
		long end = System.nanoTime();
		end = end - init;
		getresults.add(end);
		System.out.println(threadName + "  get " + element.toString());
		
		Random randomGenerator = new Random();
		try {
			Thread.sleep(randomGenerator.nextInt(5));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		init = System.nanoTime();
		resPool.put(element);
		end = System.nanoTime();
		end = end - init;
		putresults.add(end);
		
		
		System.out.println(threadName + "  put  " + element.toString());
		
		finishedThreadNumber.incrementAndGet();
		
	}

}
